<?php

namespace App\Http\Controllers\GarbageCollect;

use App\Models\Garbage\GarbageDissminateNews;
use App\Models\Garbage\GarbageGoodKind;
use App\Models\Garbage\GarbagePoint;
use App\Models\Garbage\GarbageTestUser;
use http\Env\Response;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Database\QueryException;
use Illuminate\Support\Facades\Facade;
use DB;
use Validator;

/**
 * @group 垃圾分类->会员管理->积分管理查询接口(只有查询接口)
 *
 * 采用 restful api
 *
 * Body Parameters 表示请求的所需要的参数
 *
 * Query Parameters 表示返回参数的名称
 */

class GarbagePointUsersController extends Controller
{
    /**
     * 信息展
     * @queryParam no 流水编号.
     * @queryParam event_point 积分 有正有负.
     * @queryParam final_point 变动后用户的积分.
     * @queryParam event 变动事件.
     * @queryParam user_id 用户id.
     * @queryParam user 用户详细信息.
     * @queryParam num 投递数.
     *
     * @bodyParam search string optional 搜索用户微信号(模糊)
     * @bodyParam start_time datetime optional 搜索起始时间
     * @bodyParam end_time datetime optional 搜索结束时间
     */
    public function index(Request $request){
        $query = GarbagePoint::select(DB::raw('user_id, max(created_at) as time,count(*) as num'))->groupBy('user_id')
            ->whereHas('user',function ($q) use ($request){
               $request->has('search') && $q->where("nickname","LIKE","%{$request->get('search')}%");
            })->where(function ($q) use ($request){
                $request->has('start_time') && $q->where('created_at','>=',$request->get('start_time'));
                $request->has('end_time') && $q->where('created_at','<=',$request->get('end_time'));
            })->with('user');
        return response()->json(encodeMsg(pageQuery($query,$request),200),200);
    }
}